package com.auxgroup.hr.front.site.co.process;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.enums.WfTypeEnum;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCPersonalData;
import com.auxgroup.hr.front.dao.domain.PsCWfDrftHdr;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.personnal.PersonalDataService;
import com.auxgroup.hr.front.service.process.WfDrftHdrService;
import com.auxgroup.hr.front.site.co.BaseWfCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.process.WfDrftHdrReqVO;
import com.auxgroup.hr.front.site.vo.process.WfDrftHdrRespVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 招聘录用审批流程
 *
 * @author lv-PC
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/wfDrftHdr")
public class WfDrftHdrCO extends BaseWfCO {

    private static final Logger logger = LoggerFactory.getLogger(WfDrftHdrCO.class);

    @Autowired
    public WfDrftHdrService wfDrftHdrService;

    @Autowired
    private PersonalDataService personalDataService;


    @Resource(name = "dozerMapper")
    protected IObjectMapper objectMapper;


    //权限字段
    @Override
    protected Set<String> getAuthFileds() {
        return new HashSet<>(Arrays.asList("cPayRange", "gpPaygroup", "cPayType", "cPayAnnualSal",
                "cPayAplSal", "cPayDailySal", "cFloatSplitFlg", "cPayMonavgSal", "cPayHourSal", "cSalMonFloat",
                "cPayFixedPct",
                "cPayFloatPct", "cSbPayBase", "cPerformResult", "cPayBonuxNum"));

    }


    /**
     * 获取招聘录用审批流程信息
     *
     * @param cTransactionNbr
     * @return
     */
    @GetMapping(value = "/getDrftInfo")
    public BaseResponseVO getOne(String cTransactionNbr, @SessionGain("user") UserDTO user) {
        logger.info("getWfDrftHdr param= {}", cTransactionNbr);
        if (StringUtils.isBlank(cTransactionNbr)) {
            return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL);
        }
        PsCWfDrftHdr drftHdr = wfDrftHdrService.getDrftHdr(cTransactionNbr);
        if (drftHdr == null) {
            return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
        }
        WfDrftHdrRespVO respVo = objectMapper.map(drftHdr, WfDrftHdrRespVO.class);

        //设置部门全称
        respVo.setDetpDescr(sysDictService.getDetpFullName(drftHdr.getSetid(), drftHdr.getDeptid()));
        //岗位性质短描述
        respVo.setcJobType2(sysDictService.getJobPropertyDescshort(respVo.getcJobType2()));
        //岗位名称
        respVo.setJobcodeDescr(sysDictService.getJobDesc(drftHdr.getcDrftSetid(), drftHdr.getJobcode()));
//		respVo.setJobcodeDescr(sysDictService.getJobDesc(drftHdr.getSetid(), drftHdr.getcBsJobCode()));
        respVo.setcPayType(getDictValue("C_PAY_TYPE", drftHdr.getcPayType()));
        respVo.setcPerformResultValue(getDictValue("C_PERFORM_RESULT", drftHdr.getcPerformResult()));

        //公司描述
        respVo.setcDrftBusUnit(sysDictService.getBusinessName(drftHdr.getcDrftBusUnit()));
        //业务单位描述
        respVo.setcWfBu(sysDictService.getBusinessName(drftHdr.getcWfBu()));
        //拼接描述 姓名_事业部_所属部门_岗位描述
        respVo.setTempCName(sysDictService.getJobJointDesc(drftHdr.getEmplid2()));

        respVo.setGpPaygroupValue(sysDictService.getPygrp(drftHdr.getGpPaygroup()));
        respVo.setRsSilCdValue(sysDictService.getSilDescr(drftHdr.getRsSilCd()));
        //导师名字
        PsCPersonalData daoshi = personalDataService.getPersonalData(drftHdr.getcEmplid2());
        if (daoshi != null) {
            respVo.setcEmplid2Name(daoshi.getName());
        }

//        //获取分权信息
//        List<String> fqyyInfo = sysDictService.getFqyyInfo(drftHdr.getcTransactionNbr());
//        if (fqyyInfo != null && fqyyInfo.size() > 1) {
//            respVo.setFqyyFlag(fqyyInfo.get(0).equals("Y") ? "有" : "无");
//            respVo.setFqyyMsg(fqyyInfo.get(1));
//        } else {
//            respVo.setFqyyFlag("无");
//        }
//        Integer processToatalSum = sysDictService.getProcessTotalSum();
//        Integer nofqyySum = sysDictService.getNoFqyySum();
//        if (processToatalSum == 0) {
//            respVo.setNoFqyySum("0");
//            respVo.setNoFqyyRatio("0");
//        } else {
//            respVo.setNoFqyySum(nofqyySum.toString());
//            respVo.setNoFqyyRatio(Math.round(nofqyySum * 10000.0 / processToatalSum) / 100 + "");
//        }

        fillBaseWfInfo(user, drftHdr, respVo);
        return buildRespone(respVo);
    }


    /**
     * 保存并审批招聘录用流程
     *
     * @param reqVo
     * @param user
     * @param binding
     * @return
     */
    @PostMapping(value = "/approveDrft")
    public BaseResponseVO approveDrft(WfDrftHdrReqVO reqVo, @SessionGain("user") UserDTO user, BindingResult binding) {
        // 表单校验
        super.assertValidation(binding);
        PsCWfDrftHdr drftHdr = wfDrftHdrService.getDrftHdr(reqVo.getcTransactionNbr());
        if (drftHdr == null) {
            return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
        }
        //设置数据
//		super.fillParams(drftHdr, user, reqVo);
//        //保存数据
//        wfDrftHdrService.saveDrftHdr(drftHdr);
        //审批流程
        return requestWs(WfTypeEnum.WF_STYPE_DRFT.getType(), reqVo, user);
    }

}
